package com.xxxt.jxj.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxt.entity.ClassGrade;
import com.xxxt.entity.College;
import com.xxxt.entity.Grade;
import com.xxxt.entity.School;
import com.xxxt.entity.Specialty;
import com.xxxt.entity.Student;
import com.xxxt.jxj.service.ClassGradeService;
import com.xxxt.jxj.service.CollegeService;
import com.xxxt.jxj.service.GradeService;
import com.xxxt.jxj.service.SchoolService;
import com.xxxt.jxj.service.SpecialtyService;
import com.xxxt.utils.UUIDUtil;

@Controller
@RequestMapping("/college")
public class CollegeController {
	@Resource
	private CollegeService collegeService;
	@Resource
	private SchoolService schoolService;
	@Resource
	private SpecialtyService specialtyService;
	@Resource
	private GradeService gradeService;
	@Resource
	private ClassGradeService classGradeService;
	/***
	 * 插入学院
	 * @param schoolId
	 * @param collegeId
	 * @param collegeName
	 * @return
	 */
	@RequestMapping(value="/insertCollege",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject insertCollege(String schoolId,String collegeName){
		JSONObject json = new JSONObject();
		try {
			School school = schoolService.selcetOneSchool(schoolId);
			if (school==null) {
				json.put("status", 2);
				json.put("message", "当前学校不存在,添加失败");
				return json;
			}				
			List<College> list = collegeService.selcetAllCollegeBySchoolId(schoolId);//学院对应的学校下的遍历
			College college = new College();
			college.setCollegeId(UUIDUtil.getUUID());
			college.setCollegeName(collegeName);
			college.setSchool(school);
			for (College college2 : list) {			
				if (college2.getCollegeName()==collegeName||college2.getCollegeName().equals(collegeName)) {
					json.put("status", -2);
					json.put("message", "当前学校已有相同的学院名称存在，请不要重复添加！");
					return json;			
				}
			}
			boolean insertCollege = collegeService.insertCollege(college);
			if (insertCollege) {
				json.put("status", 0);
				json.put("message", "添加成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "添加失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "添加失败");
			return json;	
		}		
		return json;		
	}
	/**'
	 * 查询单个学院信息
	 * @param collegeId
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/selcetOneCollegeByCollegeId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetOneCollegeByCollegeId(String collegeId,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			College selcetOneCollegeByCollegeId = collegeService.selcetOneCollegeByCollegeId(collegeId);
			if (selcetOneCollegeByCollegeId!=null) {
				session.setAttribute("selcetOneCollegeByCollegeId", selcetOneCollegeByCollegeId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("college", selcetOneCollegeByCollegeId);			
			}else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;	
		}	
		return json;				
	}
	/**
	 * 修改单个学院信息
	 * @param collegeId
	 * @param schoolId
	 * @param collegeName
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/updateCollege",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject updateCollege(String schoolId,String collegeName,HttpSession session){
		JSONObject json = new JSONObject();
		try {
			College college=(College) session.getAttribute("selcetOneCollegeByCollegeId");

			School school = schoolService.selcetOneSchool(schoolId);
			if (school==null) {
				json.put("status", -1);
				json.put("message", "抱歉，当前列表中没有该学校！");
				return json;
			}
			List<College> selcetAllCollegeBySchoolId = collegeService.selcetAllCollegeBySchoolId(schoolId);
			for (College college2 : selcetAllCollegeBySchoolId) {
				
				if (college2.getCollegeName()==collegeName||college2.getCollegeName().equals(collegeName)) {
					json.put("status", -3);
					json.put("message", "抱歉，该学校已经有相同的学院名称咯！");
					return json;
				}
			}
	
			boolean updateCollege = collegeService.updateCollege(collegeName, school.getSchoolId(), college.getCollegeId());
			if (updateCollege) {
				json.put("status", 0);
				json.put("message", "修改成功");
			}
			else {
				json.put("status", 1);
				json.put("message", "修改失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;	
			
		}		
		return json;
		}	
	/**
	 * 删除单个学院信息
	 * @param collegeId
	 * @return
	 */
	@RequestMapping(value="/deleteCollege",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject deleteCollege(String collegeId){
		JSONObject json = new JSONObject();
		try {
			boolean deleteCollege = collegeService.deleteCollege(collegeId);
			if (deleteCollege) {
				json.put("status", 0);
				json.put("message", "删除成功");
			}
			else {
				json.put("status", -1);
				json.put("message", "删除失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;	
		}		
		return json;
	}
	/**
	 * 根据学校id 分页查询学院信息
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selcetAllPageCollegeBySchoolId",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selcetAllPageCollegeBySchoolId(String schoolId,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {
			PageHelper.startPage(pageNum, pageSize);
			List<College> selcetAllPageCollegeBySchoolId = collegeService.selcetAllPageCollegeBySchoolId(schoolId, pageNum, pageSize);
			if (selcetAllPageCollegeBySchoolId.size()>0) {
				PageInfo<College> pageInfo=new PageInfo<>(selcetAllPageCollegeBySchoolId);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
			return json;	
		}		
		return json;
	}
	/**
	 * 根据老师和学院查询出对应学生分页
	 * @param schoolId
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value="/selectStudentByTnoAndCollege",method=RequestMethod.POST)
	@ResponseBody
	public JSONObject selectStudentByTnoAndCollege(String collegeId ,String tno,int pageNum,int pageSize){
		JSONObject json = new JSONObject();
		try {	
			
			List<Specialty> selcetAllSpecialtyByCollegeId = specialtyService.selcetAllSpecialtyByCollegeId(collegeId);
			
			List<Grade> selectStudentByTnoAndCollege = specialtyService.selectStudentByTnoAndCollege(selcetAllSpecialtyByCollegeId);
			
			List<ClassGrade> selectStudentByTnoAndSpecialty = gradeService.selectStudentByTnoAndSpecialty(selectStudentByTnoAndCollege);	
			if (selcetAllSpecialtyByCollegeId.size()<=0) {
				json.put("status", -5);
				json.put("message", "当前对应专业没有你的学生");	
				return json;
			}			
			if (selectStudentByTnoAndCollege.size()<=0) {
				json.put("status", -4);
				json.put("message", "当前对应年级没有你的学生");	
				return json;
			}
			if (selectStudentByTnoAndSpecialty.size()<=0) {
				json.put("status", -3);
				json.put("message", "当前对应班级没有你的学生");
				return json;
			}
			PageHelper.startPage(pageNum, pageSize);
			List<Student> selectStudentByTnoAndList = classGradeService.selectStudentByTnoAndList(tno, selectStudentByTnoAndSpecialty,pageNum,pageSize);
			if (selectStudentByTnoAndList.size()>0) {
				PageInfo<Student> pageInfo=new PageInfo<>(selectStudentByTnoAndList);
				json.put("status", 0);
				json.put("message", "查询成功");
				json.put("pageInfo", pageInfo);
			}
			else if (selectStudentByTnoAndList.size()==0) {
				json.put("status", -2);
				json.put("message", "当前没数据");
			}
			else {
				json.put("status", -1);
				json.put("message", "查询失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			json.put("status", -1);
			json.put("message", "查询失败");
		}
		return json;
	}
}
